package com.viewplug.view.custom.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

/**
 * Created by mac_wk on 15-1-6.
 */
public class StatisticsArc extends View {
    private Paint paint_black, paint_white;
    private RectF rectf;
    private int blackColor = 0x70000000; // 底黑色
    private int whiteColor = 0xddffffff; // 白色
    private float arc_stroke = 2f;
    private float text_stroke = 2f;
    private float textSise = 60f;
    //画出的最大值
    private int max=100;



    //进度条的角度
    private float arc_y=0;
    private int nowCount=0;
    public StatisticsArc(Context context) {
        this(context, null);

    }



    public StatisticsArc(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StatisticsArc(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
        paint_black = new Paint();
        paint_black.setAntiAlias(true);
        paint_black.setColor(blackColor);
        paint_black.setStrokeWidth(arc_stroke);
        paint_black.setStyle(Paint.Style.STROKE);
        paint_white = new Paint();
        paint_white.setAntiAlias(true);
        paint_white.setColor(whiteColor);
        paint_white.setTextSize(textSise);
        paint_white.setStrokeWidth(arc_stroke);
        paint_white.setTextAlign(Paint.Align.CENTER);
        paint_white.setStyle(Paint.Style.STROKE);
        rectf = new RectF();
        this.getViewTreeObserver().addOnPreDrawListener(
                new ViewTreeObserver.OnPreDrawListener() {
                    public boolean onPreDraw() {
                        new ArcThread().start();
                        getViewTreeObserver().removeOnPreDrawListener(this);
                        return false;
                    }
                });
    }

    private float font_width;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int max_arc = getMeasuredHeight()>getMeasuredWidth()?getMeasuredHeight():getMeasuredWidth();
        rectf.set( arc_stroke,arc_stroke, max_arc-arc_stroke, max_arc-arc_stroke);
        font_width =  max_arc/2;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(rectf, -90, 360, false, paint_black);
        canvas.drawArc(rectf, -90, arc_y, false, paint_white);
        String str = "" + nowCount;

        canvas.drawText(str,  font_width, font_width+arc_stroke, paint_white);
    }

    private class ArcThread extends  Thread{
        private int s=0;
        @Override
        public void run() {
            super.run();
            while (true)
            {
                switch (s)
                {
                    case 0:
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        s = 1;
                        break;
                    case 1:
                        try {
                            Thread.sleep(15);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        nowCount++;
                        arc_y+=3.6f;

                        break;
                }
                postInvalidate();
                if (nowCount>=max)
                break;
            }
        }
    }
    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }
}
